package com.example.bgzk;


import android.content.Context;
import android.os.Message;
import android.os.Handler;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;


public class UdpServer {

    public static final String TAG = "ControlActivity";
    public boolean is_start =true;
    //端口和IP
    public  String  Local_IP =  "192.168.10.60"; // 本机IP
    public  String  Broadcast_IP = "192.168.10.68"; // 目的IP
    public  int  Broadcast_PORT = 60100; // 发送、接收端口号

    public InetAddress inetAddress = null;

    // 发送的数据报套接字和数据报包
    public DatagramSocket sendSocket = null;
    public DatagramPacket sendDataPacket;
    public DatagramPacket sendStatePacket;

    // 接收的数据报套接字和数据包
    public DatagramSocket receiveSocket = null;
    public DatagramPacket receivePacket;



    // 发送的状态包和数据包
    public byte[] STATE_PACK;
    public byte[] DATA_PACK;

    // 接收数据区 、 发送数据区
    byte[] receiveData;
    byte[] sendData;

    // 上下文和handler
    public Context mcontext;
    public Handler mhandler;



    //--------- 构造方法---------
    public UdpServer(Context context, Handler handler) {
        // 广播
        try {
            inetAddress = InetAddress.getByName(Broadcast_IP);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        // 接收
        receiveData = new byte[100];
        try{
            receiveSocket = new DatagramSocket(Broadcast_PORT);
        } catch (SocketException e) {
            e.printStackTrace();
        }
        receivePacket = new DatagramPacket(receiveData, receiveData.length);

        //状态包初始化 ----总长度6
        STATE_PACK = new byte[12];
        // 包头 AA55
        STATE_PACK[0] = -86;
        STATE_PACK[1] = 85;
        // IP 61--70
        STATE_PACK[2] = 0;
        // 命令
        STATE_PACK[3] = 0;
        // 动态
        STATE_PACK[4] = 0;
        // 类型
        STATE_PACK[5] = 0;
        // 样式
        STATE_PACK[6] = 0;


        // 发送
        sendData = new byte[1024];
        try{
            sendSocket = new DatagramSocket();
        } catch (SocketException e) {
            e.printStackTrace();
        }
        sendStatePacket = new DatagramPacket(STATE_PACK,STATE_PACK.length,inetAddress,Broadcast_PORT);
//        sendDataPacket = new DatagramPacket(DATA_PACK,DATA_PACK.length,inetAddress,Broadcast_PORT);
        // 上下文和Handler
        mcontext = context;
        mhandler = handler;
    }  // UdpServer


    //-------发送状态包-------
    public void SendStatePack(int cmd, int state, int bulletType, int targetType){


       
        STATE_PACK[3] = (byte)cmd;
        
        STATE_PACK[4] = (byte)state;
       
        STATE_PACK[5] = (byte)bulletType;
        
        STATE_PACK[6] = (byte)targetType;

          try{
              sendSocket.send(sendStatePacket);
          } catch (IOException e) {
              e.printStackTrace();
          }
    }

    //--------发送数据包-------
    public void SendDataPack(){

    }


    //-------接收数据包-------
    public void ReceiveDataPack (){

        try{
              receiveSocket.receive(receivePacket);
        } catch (IOException e) {
           e.printStackTrace();
         }

        // ------>>>检测报文的合法性

        // 1、判断是否有数据
        if(receivePacket == null) {
            System.out.println("------没有数据！！！");
            return;
        }
        // 2、判断IP
        String recvIp;
        recvIp = receivePacket.getAddress().toString().substring(1);
        if(Broadcast_IP.equals(recvIp) == false){
            System.out.println("******IP不一样！");
            return;
        }
        // 3、判断包头
        if(receiveData[0] != -86 && receiveData[1] != 85){
            System.out.println("******包头不符！");
            return;
        }


        Message message = new Message();
        switch (receiveData[3]){
            case 0:
                message.what = 1;
                mhandler.sendMessage(message);
                System.out.println("接收到命令");
                break;

        }


    }
    //------结束线程--------
    public void DestroySocket() {
        sendSocket.close();
        receiveSocket.close();
    }
}

